﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractForeEnd;
using HamcoWcs.DAL;
using HamcoWcs.Models.Common;
using HamcoWcs.Models.Exceptions;
using HamcoWcs.Models.ForeEndModels.UIPaths;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading.Tasks;

namespace HamcoWcs.Main.Controllers.Common
{
    /// <summary>
    /// 前端路径接口
    /// </summary>
    public class PathController : BasicController
    {
        #region [创建节点]

        /// <summary>
        /// 创建节点
        /// </summary>
        [HttpPost]
        public IActionResult CreateNode(UINodeCreateModel model, [FromHeader] string userName = "")
        {
            try
            {
                var resule = _pathService.CreateNode(model);
                AddOperationLog(userName, $"创建节点{model.NodeName}");
                return Json(ForeEndResult.Ok);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return CreateFailureResult();
            }
        }

        #endregion

        #region [删除节点]

        /// <summary>
        /// 删除节点
        /// </summary>
        [HttpPost]
        public IActionResult DeleteNode(UINodeDeleteModel model, [FromHeader] string userName = "")
        {
            try
            {
                var resule = _pathService.DeleteNode(model);
                AddOperationLog(userName, $"创建节点,Id:{model.Id}");
                return Json(ForeEndResult.Ok);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return DeleteFailureResult();
            }
        }

        #endregion

        #region [更新节点]

        /// <summary>
        /// 更新节点
        /// </summary>
        [HttpPost]
        public IActionResult UpdateNode(UINodeUpdateModel model, [FromHeader] string userName = "")
        {
            try
            {
                var resule = _pathService.UpdateNode(model);
                AddOperationLog(userName, $"更新节点，Id:{model.Id}");
                return Json(ForeEndResult.Ok);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return UpdateFailureResult();
            }
        }

        #endregion

        #region [查询节点]

        /// <summary>
        /// 获取节点
        /// </summary>
        [HttpPost]
        public IActionResult GetNodes(UINodeQueryModel model)
        {
            try
            {
                var resule = _pathService.GetNodes(model);
                return Json(resule, DEFAULT_JSON_SETTINGS);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return QueryFailureResult();
            }
        }

        #endregion

        #region [更新边]

        /// <summary>
        /// 更新边
        /// </summary>
        [HttpPost]
        public IActionResult UpdateEdge(UIEdgeUpdateModel model, [FromHeader] string userName = "")
        {
            try
            {
                var resule = _pathService.UpdateEdge(model);
                AddOperationLog(userName, $"更新边，Id:{model.Id}");
                return Json(ForeEndResult.Ok, DEFAULT_JSON_SETTINGS);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return UpdateFailureResult();
            }
        }

        #endregion

        #region [查询边]

        /// <summary>
        /// 查询边
        /// </summary>
        [HttpPost]
        public IActionResult GetEdges(UIEdgeQueryModel model)
        {
            try
            {
                var resule = _pathService.GetEdges(model);
                return Json(resule, DEFAULT_JSON_SETTINGS);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return QueryFailureResult();
            }
        }

        #endregion

        #region [更新路径]

        /// <summary>
        /// 更新路径
        /// </summary>
        [HttpPost]
        public IActionResult UpdatePath(UIPathUpdateModel model, [FromHeader] string userName = "")
        {
            try
            {
                var resule = _pathService.UpdatePath(model);
                AddOperationLog(userName, $"更新路径，Id:{model.Id}");
                return Json(ForeEndResult.Ok, DEFAULT_JSON_SETTINGS);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return UpdateFailureResult();
            }
        }

        #endregion

        #region [查询路径]

        /// <summary>
        /// 查询路径
        /// </summary>
        [HttpPost]
        public IActionResult GetPaths(UIPathQueryModel model)
        {
            try
            {
                var result = _pathService.GetPaths(model);
                return Json(result, DEFAULT_JSON_SETTINGS);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return QueryFailureResult();
            }
        }

        #endregion

        #region [生成路径]

        /// <summary>
        /// 生成路径
        /// </summary>
        [HttpPost]
        public IActionResult GeneratePath(UIPathGenerateToken token, [FromHeader] string userName = "")
        {
            //_pathService.GeneratePath();
            //return Json(ForeEndResult.Ok, DEFAULT_JSON_SETTINGS);
            try
            {
                if (string.IsNullOrEmpty(PATH_GENERATE_TOKEN.Token)) throw new WarningException("请先获取令牌");

                if (PATH_GENERATE_TOKEN.Compared(token.Token))
                {
                    _pathService.GeneratePath();
                    AddOperationLog(userName, $"{userName} 生成路径");
                    return Json(ForeEndResult.Ok, DEFAULT_JSON_SETTINGS);
                }
                else
                {
                    throw new WarningException("输入的令牌错误");
                }
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return QueryFailureResult();
            }
        }

        #endregion

        #region [UI添加路径]

        /// <summary>
        /// 生成路径
        /// </summary>
        [HttpPost]
        public IActionResult AddPath(UIAddPathModel model, [FromHeader] string userName = "")
        {
            //_pathService.GeneratePath();
            //return Json(ForeEndResult.Ok, DEFAULT_JSON_SETTINGS);
            try
            {
                _pathService.AddPath(model);

                return Json(ForeEndResult.Ok);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return UnknownExceptionResult();
            }
        }

        #endregion

        #region [UI添加路径]

        /// <summary>
        /// 生成短路径
        /// </summary>
        [HttpPost]
        public IActionResult AddEdge(UIAddEdgeModel model, [FromHeader] string userName = "")
        {
            //_pathService.GeneratePath();
            //return Json(ForeEndResult.Ok, DEFAULT_JSON_SETTINGS);
            try
            {
                _pathService.AddEdge(model);
                return Json(ForeEndResult.Ok);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return UnknownExceptionResult();
            }
        }

        #endregion

        #region [获取生成路径的令牌]

        /// <summary>
        /// 获取生成路径的令牌
        /// </summary>
        [HttpPost]
        public IActionResult GetGeneratePathToken([FromForm] string userName = "")
        {
            try
            {
                PATH_GENERATE_TOKEN.Update();
                AddOperationLog(userName, $"{userName}生成路径的令牌");
                return Json(new ForeEndResult(PATH_GENERATE_TOKEN.Token), DEFAULT_JSON_SETTINGS);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return QueryFailureResult();
            }
        }

        #endregion

        #region [生成路径]

        /// <summary>
        /// 同步数据库路径到内存
        /// </summary>
        [HttpPost]
        public IActionResult Synchronous([FromHeader] string userName = "")
        {
            try
            {

                _path.Synchronous();

                AddOperationLog(userName, $"{userName} 同步路径");
                return Json(ForeEndResult.Ok);
            }
            catch (WarningException ex)
            {
                return Warning(ex);
            }
            catch (Exception ex)
            {
                AddSystemLog(ex.Message);
                return Json(ForeEndResult.Fail("同步失败"));
            }
        }

        #endregion
        #region [Constructor]

        private readonly IForeEndPathService _pathService;
        private readonly IPathService _path;
        /// <summary>
        /// PathController
        /// </summary>
        public override string ControllerName => "PathController";

        /// <summary>
        /// 
        /// </summary>
        public PathController(IPathService path, IForeEndPathService pathService, ILoggerService logger, IMapper mapper, WcsDbContext wcsDbContext) : base(logger, mapper, wcsDbContext)
        {
            _path = path;
            _pathService = pathService;
        }

        /// <summary>
        /// 生成路径需要安全指令
        /// </summary>
        public static UIPathGenerateToken PATH_GENERATE_TOKEN = new UIPathGenerateToken();
        #endregion
    }
}
